package com.example.application1.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.application1.API.videoApi1;
import com.example.application1.Adapter.newsAdapter;
import com.example.application1.Adapter.videoAdapter;
import com.example.application1.Entity.videoFind.videoBean2;
import com.example.application1.Entity.videoFind.视频DTO;
import com.example.application1.R;
import com.example.application1.tools.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.Jzvd;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class videoFragment extends Fragment {

    private View view;
    private List<视频DTO> video;
    private RecyclerView videoRecycle;
    private videoAdapter videoAdapter2;
    private SwipeRefreshLayout mSwipeRefreshLayout;


    public static videoFragment newInstance() {
        videoFragment fragment = new videoFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.video_fragment,container,false);
       return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        requestVideoData();

        initToolbar();
        initView();
        videoRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        videoRecycle.addItemDecoration(new DividerGridItemDecoration(getContext()));
        videoAdapter2=new videoAdapter(getContext(),video);
        videoRecycle.setAdapter(videoAdapter2);
        handleDownPullUpdate();

    }

    private void initToolbar() {
        Toolbar toolbar = view.findViewById(R.id.my_toolbar);
        toolbar.setTitle("");
        TextView toolbar_title = view.findViewById(R.id.toolbar_title);
        toolbar_title.setText("视频中心");
    }

    public void initView(){
        mSwipeRefreshLayout = view.findViewById(R.id.refresh_layout_video);
        videoRecycle=view.findViewById(R.id.videoRecycle);
    }

    //    下拉刷新方法
    private void handleDownPullUpdate() {
        mSwipeRefreshLayout.setEnabled(true);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //被刷新时的操作
                requestVideoData();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //更新成功后设置UI，停止更新
                        videoRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
                        videoRecycle.addItemDecoration(new DividerGridItemDecoration(getContext()));
                        videoAdapter2=new videoAdapter(getContext(),video);
                        videoRecycle.setAdapter(videoAdapter2);
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                },1000);
            }
        });
    }


    public void requestVideoData(){
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl("http://c.m.163.com/recommend/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                   videoApi1 service = retrofit.create(videoApi1.class);
                    Call<videoBean2> call = service.getCall();
                    call.enqueue(new Callback<videoBean2>() {
                        @Override
                        public void onResponse(Call<videoBean2> call, Response<videoBean2> response) {
                           video=response.body().get视频();
//                            videoAdapter2=new videoAdapter(getContext(),video);
                             videoAdapter2.refresh(video);
                            System.out.println(video.get(0).getMp4_url());
                        }
                        @Override
                        public void onFailure(Call<videoBean2> call, Throwable t) {
                            System.out.println("连接失败");
            }
        });

    }

    @Override
    public void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }
}
